<?php

class DiagramController extends Controller {
    var $name = 'Diagram';
    var $uses = array('Diagram');
    var $components = array('diagram');

    function index() {
        $this->action = 'diagram';
        $this->diagram();
    }

    function diagram($status = null) {
        if ($status == 'page_edit_done') {
            $this->header_notice = __('Edit page done.');
        }
        $this->set('diagram_list', get_diagram_as_ul());
    }

    function page_edit($id = null, $action = null) {
        $id = (int)$id;
        $diagram = $this->Diagram->read('*', $id);
        if (!empty($diagram)) {
            # save edit
            if (!empty($this->data)) {
                $this->data['Diagram']['id'] = $id;
                $this->data['Diagram']['title'] = sanitize_title($this->data['Diagram']['title']);
                $this->data['Diagram']['content'] = $this->data['Posts']['post_content'];
                $this->Diagram->save($this->data);
                $redirect = '/diagram/diagram/page_edit_done';
            }
            $this->data['Diagram']['title'] = $diagram->title;
            $post_content = $diagram->content;
            $this->set('post_content', $post_content);
        } else {
            $redirect = '/diagram/';
        }
        #if is edit from the skin
        if ($action == 'edit_from_web' && !empty($redirect)) {
            if (!empty($this->referer)) {
                $redirect_to_web = $this->referer;
            } else {
                $redirect_to_web = '/';
            }
            al_redirect($redirect_to_web);
        } else if (!empty($redirect)) {
            $this->redirect($redirect);
        }
    }
}
?>
